<?php
define('MINI_RUN', true);
require '../mini.php';
/*
 * 本Ajax只用于获取数据和检测操作
 * 0 表示无法执行操作
 * null 表示已经执行了操作，但数据为空。 
 */
if (empty($_POST['op'])){
	echo 0;
	return ;
}
Mini::checkLogin();
unset($_REQUEST);
switch ($_POST['op']){
	case 'getclass':		//获取栏目数据
		echo $_POST['classid'] ? getClass($_POST['classid']) : 0;
		break;
	case 'checkdir':		//检测栏目的文件夹名
		echo $_POST['classdir'] ? checkClassDir($_POST['classdir']) : 0;
		break;
	case 'checkfilename':	//检测文章的文件名
		echo ($_POST['classid']&&$_POST['filename']) ? checkFileName($_POST) : 0;
		break;
	case 'getpagelist':		//获取文章对象列表
		echo ($_POST['pageindex']&&$_POST['pagesize']&&isset($_POST['classid'])) ? getPageList($_POST) : 0;
		break;
	case 'getclasstemp':
		echo $_POST['tempid'] ? getTempData('Mini_ClassTemp',$_POST['tempid']) : 0;
		break;
	case 'getpagetemp':
		echo $_POST['tempid'] ? getTempData('Mini_PageTemp',$_POST['tempid']) : 0;
		break;
	case 'gettagtemp':
		echo $_POST['tempid'] ? getTempData('Mini_TagTemp',$_POST['tempid']) : 0;
		break;
	case 'checkdata':		//检测资料的Key / Value
		echo ($_POST['datakey'] && $_POST['datavalue']) ? checkData($_POST) : 0;
		break;
	case 'checktempname':
		echo $_POST['tempname'] && $_POST['temp'] ? checkTempNameForAdd($_POST) : 0;
		break;
	case 'checkvarname':
		echo $_POST['varname'] ? checkVarNameForAdd($_POST) : 0;
		break;
	case 'gethome':
		echo Mini::getMiniHome();
		break;
	default:
		echo 0;		
}
return ;

/**
 *********************************************
 * 栏目操作
 *********************************************/
function getClass($classId){
	unset($_POST);
	$pdo = new MySQLPDO();
	$sql = 'SELECT * FROM Mini_Class WHERE ClassId = ? limit 1';
	$classArray = $pdo->selectSQL($sql, array($classId));
	return $classArray[0] ? json_encode($classArray[0]) : 'null';
}
function checkClassDir($classdir){
	unset($_POST);
	$pdo = new MySQLPDO();
	$sql = 'SELECT ClassId FROM Mini_Class WHERE ClassDir = ? limit 1';
	$classIdArray = $pdo->selectSQL($sql, array($classdir));
	return $classIdArray[0] ? 'OK' : 'null';	
}

/**
 *********************************************
 * 文章操作
 *********************************************/
function checkFileName($pageArray){
	unset($_POST);
	$pdo = new MySQLPDO();
	$sql = 'SELECT PageId FROM Mini_Page FROM WHERE ClassId = ? AND FileName = ? limit 1';
	$pageIdArray = $pdo->selectSQL($sql,array($pageArray['classid'],$pageArray['filename']));
	return $pageIdArray[0] ? 'OK' : 'null';	
}

function getPageList($list){
	unset($_POST);	
	$start = ($list['pageindex']-1)*$list['pagesize'];	
	$select = empty($list['classid']) ? 'SELECT p.PageId,p.Title,p.ClassId,u.UserName,p.FileName FROM Mini_Page p LEFT JOIN Mini_Admin u ON p.UserId = u.AdminId' : 'SELECT p.PageId,p.Title,p.ClassId,u.UserName,p.FileName FROM Mini_Page p LEFT JOIN Mini_Admin u ON p.UserId = u.AdminId WHERE ClassId = ?';
	$select .= ' limit '.$start.','.$list['pagesize'];	
	$paramArray = ($list['classid'])?array($list['classid']):null;
	$pdo = new MySQLPDO();
	$pageArray = $pdo->selectSQL($select,$paramArray);	
	$pathArray = array();
	if ($list['classid'])
		$pathArray[$list['classid']] = Mini::getClassPath($list['classid']);
	foreach ($pageArray as &$page){
		$page['pageurl'] = $pathArray[$page['classid']]?$pathArray[$page['classid']]:($pathArray[$page['classid']] = getClassPath($page['classid']));		
	}
	return $pageArray ? json_encode($pageArray) : 'null';
}
function getTempData($table,$tempid){
	unset($_POST);
	$pdo = new MySQLPDO();
	$sql = 'SELECT TempId,TempName,TempText FROM '.$table.' WHERE TempId = ?';
	$tempArray = $pdo->selectSQL($sql,array($tempid));
	return $tempArray ? json_encode($tempArray[0]) : 'null';	
}
/**
 *********************************************
 * 资料库操作
 *********************************************/
function checkData($dataArray){
	unset($_POST);
	$pdo = new MySQLPDO();
	$sql = 'SELECT DataId FROM Mini_Data WHERE DataValue = ?';
	$dataidArray = $pdo->selectSQL($sql,array($dataArray['datavalue']));
	if (!$dataidArray[0]){
		$sql = 'SELECT DataId FROM Mini_Data WHERE DataKey = ?';
		$dataidArray = $pdo->selectSQL($sql,array($dataArray['classkey']));
		if ($dataidArray[0])
			return 'OK';
	}
	return 'null';
}
/**
 * 添加模板时检测模板名称是否已经存在
 * @param array $temp
 */
function checkTempNameForAdd($temp){
	unset($_POST);
	$pdo = new MysqlPDO();
	$sql = 'SELECT TempId FROM Mini_'.$temp['temp'].' WHERE TempName = ?';
	$tempidArray = $pdo->selectSQL($sql,array($temp['tempname']));
	return $tempidArray[0] ? 'OK' : 'null';	
}

/**
 *添加全局变量时检测变量名称是否已经存在
 * @param array $post
 */
function checkVarNameForAdd($post){
	unset($_POST);
	$pdo = new MysqlPDO();
	$sql = 'SELECT count(*) AS ROWS FROM Mini_VarTemp WHERE VarName = ? limit 1';
	$rowsArray = $pdo->selectSQL($sql, array($post['varname']));
	return $rowsArray[0]['rows'] ? 'OK' : 'null';
}
?>